package it.ilgrv.demiurgo.persistence.tables.inscription;

import it.ilgrv.demiurgo.persistence.tables.character.Character;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * The persistent class for the partecipant database table.
 * 
 */
@Entity
@NamedQueries({
	@NamedQuery(
			name = "Partecipant.searchAll.user.character.meeting",
			query = "SELECT p\nFROM Partecipant p\nWHERE\np.user = :user\nAND p.meeting = :meeting\nAND p.character = :character"),
	@NamedQuery(
			name = "Partecipant.searchActive.user.character.meeting",
			query = "SELECT p\nFROM Partecipant p\nWHERE\np.inactive = FALSE\nAND p.user = :user\nAND p.meeting = :meeting\nAND p.character = :character") })
public class Partecipant extends it.ilgrv.demiurgo.persistence.GenericTable implements Serializable {
	private static final long			serialVersionUID	= 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int							id;

	private int							version;

	@Temporal(TemporalType.TIMESTAMP)
	private Date						date;

	private boolean						inactive;

	// bi-directional many-to-one association to Availableassociation
	@ManyToOne
	private Availableassociation	availableassociation;


	// bi-directional many-to-one association to User
	@ManyToOne
	private User						user;

	// bi-directional many-to-one association to Meeting
	@ManyToOne
	private Meeting						meeting;

	// bi-directional many-to-one association to Role
	@ManyToOne
	private Role						role;

	@ManyToOne
	private Character				character;

	/**
	 * create an active partecipant (ie. partecipant.inactive = false)
	 */
	public Partecipant() {
		setDate(new Date());
		setInactive(false);
	}

	/**
	 * return true if exist a inscription for user u for metting m
	 * 
	 * @param em
	 * @param u
	 * @param m
	 * @return
	 */
	public static boolean isActivePartecipantExist(EntityManager em, User u, Character c, Meeting m) {
		return isExist(em
							.createNamedQuery("Partecipant.searchActive.user.character.meeting",
												Partecipant.class)
							.setParameter("user", u)
							.setParameter("character", c)
							.setParameter("meeting", m)
							.getResultList());
	}
	
	/**
	 * retrieve the first active partecipant for couple user/meeting (should be
	 * only one active partecipant at same time)
	 * 
	 * @param em
	 * @param u
	 * @param m
	 * @return
	 */
	public static Partecipant getActivePartecipant(EntityManager em, User u, Meeting m) {
		return getFirstData(em
								.createNamedQuery("Partecipant.searchActive.user.character.meeting",
													Partecipant.class)
							.setParameter("user", u)
							.setParameter("meeting", m)
							.getResultList());
	}

	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getVersion() {
		return this.version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public Availableassociation getAvailableassociation() {
		return this.availableassociation;
	}

	public void setAvailableassociation(Availableassociation availableassociation) {
		this.availableassociation = availableassociation;
	}

	public User getUser() {
		return this.user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Meeting getMeeting() {
		return this.meeting;
	}

	public void setMeeting(Meeting meeting) {
		this.meeting = meeting;
	}

	public Role getRole() {
		return this.role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public boolean isInactive() {
		return inactive;
	}

	public void setInactive(boolean inactive) {
		this.inactive = inactive;
	}

	public Character getCharacter() {
		return character;
	}

	public void setCharacter(Character character) {
		this.character = character;
	}

}